#!/bin/sh
# The next line is executed by /bin/sh, but not Tcl \
exec tclsh $0 ${1+"$@"}

# Copied from OpenSTA/test/regression
# Copyright (c) 2021, Parallax Software, Inc.
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

# Usage: regression [openroad | openroad_test | flow | tool]...
# where tool is name of an openroad tool (see define_tool_scripts below).

# Directory containing tests.
set test_dir [file dirname [file normalize [info script]]]
set openroad_dir [file dirname $test_dir]

source [file join $test_dir "regression.tcl"]
source [file join $test_dir "regression_tests.tcl"]

proc define_tool_script { tool script } {
  global tool_scripts
  dict set tool_scripts [string tolower $tool] $script
}

proc run_flow_tests { } {
  global tool_errors

  puts "------------------------------------------------------"
  puts "Flow"
  set flow_tests [group_tests "flow"]
  if { $flow_tests != {} } {
    incr tool_errors [regression_body $flow_tests]
  }
}

################################################################

set tool_errors 0

if { $argv == {} } {
  # Run all tool Unit tests
  puts "Nothing specified"
} else {
  foreach arg $argv {
    set arg [string tolower $arg]
    set matched 0
    # flow
    if { [string equal -nocase $arg "flow"] } {
      run_flow_tests
      set matched 1
    }

    # <openroad_test>
    set ord_tests {}
    foreach test [group_tests "all"] {
      if { [string match -nocase $arg $test] } {
	lappend ord_tests $test
	set matched 1
      }
    }
    if { $ord_tests != {} } {
      incr tool_errors [regression_body $ord_tests]
    }

    if { !$matched } {
      puts "$arg is not an openroad test or tool name."
      incr tool_errors
    }
  }
}

puts "------------------------------------------------------"
if { $tool_errors } {
  puts "Found $tool_errors tool regression failures."
} else {
  puts "Passed all tool regressions."
}

exit $tool_errors

# Local Variables:
# mode:tcl
# End:
